package com.talent.jiwen_teacher.order;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseFragment;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.OrderFinishBean;
import com.talent.jiwen_teacher.http.result.OrderItemBean;
import com.talent.jiwen_teacher.util.DateUtils;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;


/**已取消课程列表
 *
 */
public class CancelOrderFragment extends BaseFragment {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;

    private List<OrderItemBean> dataList = new ArrayList<>();

    @Override
    protected void initData() {

        noDataIv.setImageResource(R.mipmap.img_no_unfinish_order);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new CommonAdapter<OrderItemBean>(getActivity(), R.layout.item_order_new, dataList) {
            @Override
            protected void convert(ViewHolder holder, final OrderItemBean data, int position) {

                TextView txt_pin=holder.getView(R.id.txt_pin);
                if(data.teachingType==1){
                    txt_pin.setVisibility(View.GONE);
                }else{
                    txt_pin.setVisibility(View.VISIBLE);
                }

                TextView statusTv = holder.getView(R.id.txt_pay_state);
                switch (data.orderStatus){
                    case 8:
                        statusTv.setTextColor(ContextCompat.getColor(getActivity() , R.color.red));
                        statusTv.setText("异常单(学生未进入)");
                        break;
                    case 10:
                        statusTv.setTextColor(ContextCompat.getColor(getActivity() , R.color.red));
                        statusTv.setText("异常单(教师未进入)");
                        break;
                    default:
                        statusTv.setTextColor(ContextCompat.getColor(getActivity() , R.color.color_FA7D31));
                        statusTv.setText(getContext().getString(R.string.wait_pay));
                        break;
                }

                ImageView head=holder.getView(R.id.img_head);
                ImageUtil.loadCircleImageWithDefault(getActivity(),data.studentHeadImage,R.mipmap.img_cert_head,head);
                holder.setText(R.id.txt_name,data.studentName);
                holder.setText(R.id.txt_time, DateUtils.ms2Date(data.createTime));
                holder.setText(R.id.txt_no,data.orderNumber);
                holder.setText(R.id.txt_money,"¥"+data.personalPrice);
                holder.setText(R.id.txt_use_time, DateUtils.getDateDistance(data.endTime-data.startTime));

            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                index=1;
                dataList.clear();
                getOrderList();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                index++;
                getOrderList();

            }
        });
        refreshLayout.autoRefresh();
    }


    int index=1;

    /**
     * 获取教师待上课程
     */
    private void getOrderList() {
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", SPUtil.stringOut(SPConstant.USER_ID));
        map.put("orderStatus", ""+6);
        map.put("startIndex",""+index);//startIndex

        Observable observable = ApiFactory.getEducationApiSingleton().getCompletedOrderList(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<OrderFinishBean>(getActivity()) {
            @Override
            protected void _onNext(OrderFinishBean orderFinishBean) {
//                dataList.clear();
                refreshLayout.finishRefresh();
                if(orderFinishBean!=null && orderFinishBean.getOrderList()!=null){
                    dataList.addAll(orderFinishBean.getOrderList());
                    recyclerView.getAdapter().notifyDataSetChanged();
                    if(orderFinishBean.getOrderList().size()<10){
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }

                if (Validators.isEmpty(dataList)){
                    noDataIv.setVisibility(View.VISIBLE);
                }else{
                    noDataIv.setVisibility(View.GONE);
                }
            }

            @Override
            protected void _onError(String message) {
                refreshLayout.finishRefresh();
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, false);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_no_finish;
    }
}
